1. Field of the Invention
The present invention relates to operating systems for computers. More specifically, the present invention relates to a method and an apparatus that facilitates sharing objects between multiple processes, wherein the objects contain embedded references to non-shared objects.
2. Related Art
The idea of sharing executable code among operating system processes gained widespread acceptance in mid-1980s, with the introduction of shared libraries. The use of shared libraries lowers the system-wide memory footprint and enables faster application start-up. Moreover, providing support for sharing libraries at the operating system level frees programmers from having to code the sharing themselves. Today, shared libraries are an entrenched concept, available in optimized forms in most operating systems.
As a result of the popularity of the Java(trademark) programming language, many computers have the Java virtual machine (JVM(trademark)) installed on them. (The terms xe2x80x9cSun Microsystemsxe2x80x9d, xe2x80x9cJavaxe2x80x9d, xe2x80x9cJava virtual machinexe2x80x9d and xe2x80x9cJVMxe2x80x9d are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.) Each such computer may be running many Java applications at any given time. One may ask whether in these settings the concept of sharing executable code (which in the context of the JVM includes runtime representation of classes, methods, their bytecodes, and compiled code of methods generated by a just-in-time or dynamic compiler) across multiple virtual machines might be as beneficial for the scalability of the JVM as the notion of shared libraries has been for operating systems.
Several characteristics of the Java programming language and of the JVM make sharing of executable code challenging. First, dynamic class loading makes the granularity of sharable unit smallxe2x80x94at most a class. Second, the size and format of executable code evolve during program execution as the JVM identifies target methods for optimizations and compiles selected methods. Third, executable code is often intertwined with the runtime state of a program. For example, references to objects subject to garbage collection are often embedded in executable code.
Despite these challenges, sharing of metadata remains attractive because of its potential to decrease the memory footprint of virtual machines and to factor out costs related to on-demand class loading (e.g., parsing, verification, dynamic link resolution) and to architecture neutrality of class files (e.g., runtime quickening of interpreted code and runtime compilations).
What is needed is method and an apparatus that facilitates sharing code between multiple virtual machines and/or processes in a manner that efficiently deals with the above-described challenges.
One embodiment of the present invention provides a system that facilitates sharing an object containing embedded references to non-shared objects, which may be located at different virtual memory addresses in different processes. During operation, the system looks up the object in a shared memory area, wherein the shared memory area is mapped to the same virtual address by all sharing processes. If the object does not exist in the shared memory area, the system creates the object within the shared memory area, and sets embedded references in the object to point to entries in an indirection table located at a second virtual address, wherein each sharing process maintains its own private version of the indirection table at the same second virtual address. Next, the system performs a private initialization operation on the object for a specific process by setting references in the private version of the indirection table for the specific process to point to non-shared objects located in a private memory area for the specific process. In this way, the system allows each sharing process to maintain its own private versions of the non-shared objects.
In a variation on this embodiment, setting embedded references in the object to point to entries in the indirection table involves first allocating space in the indirection table for the embedded references.
In a variation on this embodiment, prior to looking up the object, the system initializes the shared memory area so that the shared memory area can be accessed by all sharing processes. The system also ensures that all sharing processes map the shared memory area to the same first virtual address. It also ensures that all sharing processes map their private versions of the indirection table to the same second virtual address.
In a variation on this embodiment, the object includes a software component.
In a variation on this embodiment, the object includes class metadata. In this variation, the object lookup takes place during a dynamic class loading process.
In a variation on this embodiment, the object includes a compiled method that is compiled from platform-independent code into processor native code. In this variation, the object lookup takes place during a dynamic compilation process.
In a variation on this embodiment, each of the sharing processes executes a separate instance of a platform-independent virtual machine.